package com.example.service.impl;

import com.example.mapper.*;
import com.example.pojo.OutProduction;
import com.example.pojo.PageBean;
import com.example.pojo.Payment;
import com.example.pojo.Quality;
import com.example.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.List;
import java.util.Random;

@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private PayMapper payMapper;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private BaobiaoMapper baobiaoMapper;
    @Autowired
    private ProductionMapper productionMapper;
    @Autowired
    private OutProductionMapper outProductionMapper;


    @Override
    public PageBean page(Integer page, Integer pageSize, String customername, String paymentstatus, String productName, String status, LocalDate begin, LocalDate end, LocalDate begin1, LocalDate end1) {
        Long count = payMapper.count(customername,paymentstatus,productName,status,begin,end,begin1,end1);//总数返回
        Integer start=(page-1)*pageSize;//计算起始位置
        List<Payment> payments = payMapper.page(start, pageSize,customername,paymentstatus,productName,status,begin,end,begin1,end1);//该页记录数返回

        PageBean pageBean = new PageBean(count, payments);
        return pageBean;
    }

    @Override
    public void add(Payment payment) {
        payment.setTotal(payment.getPrice()*payment.getNum());
        Random random = new Random();

        // 生成随机整数
        int randomNumber = random.nextInt(1000)+101;
        payment.setProductID(randomNumber);
        payMapper.add(payment);
    }

    @Override
    public void delete(int[] id) {
        planMapper.deletes(id);
        baobiaoMapper.deletes(id);
        payMapper.delete(id);
    }

    @Override
    public String check(Payment payment) {
        if(productionMapper.existsByName(payment.getProductName())==false){
            return "库存中无该产品，请进行生产";
        }
        int cun=productionMapper.getNum(payment.getProductName());
        if(cun>=payment.getNum()){
            return "产品库存充足,可以进行订单提交";
        }else {
            return "库存不充足,请进行生产保单添加";
        }
    }

    @Override
    public void chu(Payment payment) {
        String status="已发货";
        payMapper.updates(payment.getId(),status);
        payMapper.setaa(payment.getId(),LocalDate.now());
        Payment payment1=payMapper.seletone(payment.getId());
        OutProduction outProduction=new OutProduction();
        outProduction.setPname(payment1.getProductName());
        outProduction.setNum(payment1.getNum());
        outProduction.setPrice(payment1.getPrice());
        outProduction.setTime(LocalDate.now());
        outProduction.setTotal(payment1.getTotal());
        outProduction.setNote("生产保单，完成订单"+payment1.getId()+"收入"+payment1.getTotal()+"元");
        outProductionMapper.add(outProduction);
        productionMapper.updatef(payment1.getProductName(),payment1.getNum());
    }

    @Override
    public List<Payment> getAll() {
        return payMapper.geAll();
    }

    @Override
    public PageBean page1(Integer page, Integer pageSize, String customername, String paymentstatus, String productName, String status, LocalDate begin, LocalDate end, LocalDate begin1, LocalDate end1) {
        Long count = payMapper.count(customername,paymentstatus,productName,status,begin,end,begin1,end1);//总数返回
        Integer start=(page-1)*pageSize;//计算起始位置
        List<Payment> payments = payMapper.page1(start, pageSize,customername,paymentstatus,productName,status,begin,end,begin1,end1);//该页记录数返回

        PageBean pageBean = new PageBean(count, payments);
        return pageBean;
    }

    @Override
    public PageBean page2(Integer page, Integer pageSize, String customername, String paymentstatus, String productName, String status, LocalDate begin, LocalDate end, LocalDate begin1, LocalDate end1) {
        Long count = payMapper.count(customername,paymentstatus,productName,status,begin,end,begin1,end1);//总数返回
        Integer start=(page-1)*pageSize;//计算起始位置
        List<Payment> payments = payMapper.page2(start, pageSize,customername,paymentstatus,productName,status,begin,end,begin1,end1);//该页记录数返回

        PageBean pageBean = new PageBean(count, payments);
        return pageBean;
    }

    @Override
    public PageBean page2s(Integer page, Integer pageSize, LocalDate date) {
        Long count = payMapper.count2s(date);//总数返回
        Integer start=(page-1)*pageSize;//计算起始位置
        List<Payment> payments = payMapper.page2s(start, pageSize,date);//该页记录数返回

        PageBean pageBean = new PageBean(count, payments);
        return pageBean;
    }
}
